# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=pip
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=20.0.2
pkgrel=1
pkgdesc="An easy_install replacement for installing pypi python packages (mingw-w64)"
arch=('any')
license=('MIT')
url="https://pip.pypa.io/"
depends=("${MINGW_PACKAGE_PREFIX}-python-appdirs"
         "${MINGW_PACKAGE_PREFIX}-python-cachecontrol"
         "${MINGW_PACKAGE_PREFIX}-python-colorama"
         "${MINGW_PACKAGE_PREFIX}-python-contextlib2"
         "${MINGW_PACKAGE_PREFIX}-python-distlib"
         "${MINGW_PACKAGE_PREFIX}-python-html5lib"
         "${MINGW_PACKAGE_PREFIX}-python-lockfile"
         "${MINGW_PACKAGE_PREFIX}-python-msgpack"
         "${MINGW_PACKAGE_PREFIX}-python-packaging"
         "${MINGW_PACKAGE_PREFIX}-python-pep517"
         "${MINGW_PACKAGE_PREFIX}-python-progress"
         "${MINGW_PACKAGE_PREFIX}-python-pyparsing"
         "${MINGW_PACKAGE_PREFIX}-python-pytoml"
         "${MINGW_PACKAGE_PREFIX}-python-requests"
         "${MINGW_PACKAGE_PREFIX}-python-retrying"
         "${MINGW_PACKAGE_PREFIX}-python-six"
         "${MINGW_PACKAGE_PREFIX}-python-webencodings")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx")
install=${_realname}3-${CARCH}.install
source=(${_realname}-${pkgver}.tar.gz::https://github.com/pypa/pip/archive/${pkgver}.tar.gz)
noextract=(${_realname}-${pkgver}.tar.gz)
sha256sums=('00bdc118df4552f654a5ccf0bd3ff1a7d101ee7d7ac899fe9a752363b3f2f070')

shopt -s extglob

prepare() {
  # work around symlinks in pip package that can cause problems with some building
  # where dirs are not removed after being used.
  rm -rf ${_realname}-${pkgver}/tests/data/packages/symlinks/docs || true 2> /dev/nul
  bsdtar zxf ${_realname}-${pkgver}.tar.gz || true

  # specify that third-party stuff should not be bundled.  This is from Archlinux
  rm -rf ${_realname}-${pkgver}/src/pip/_vendor/!(__init__.py)
  sed -i -e 's/DEBUNDLED = False/DEBUNDLED = True/' \
         -e '/cachecontrol/a\    vendored("appdirs")' \
        ${_realname}-${pkgver}/src/pip/_vendor/__init__.py

  rm -rf python-build-${CARCH}| true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
}

build() {
  msg "Python build for ${CARCH}" 
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  local _mingw_prefix=$(cygpath -wm ${MINGW_PREFIX})

  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build -v

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|/usr/bin/env |${MINGW_PREFIX}|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done

  install -D -m644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
